package com.youpai.platform.chat.service.service.impl;


import com.youpai.platform.chat.service.constant.ChatConstants;
import com.youpai.platform.chat.service.converter.ChatMessageConverter;
import com.youpai.platform.chat.service.entity.dto.ChatMessageDTO;
import com.youpai.platform.chat.service.entity.po.ChatMessagePO;
import com.youpai.platform.chat.service.mapper.ChatMessageMapper;
import com.youpai.platform.chat.service.service.ChatMessageService;
import com.youpai.platform.chat.service.util.ChatMessageRedisKeyUtil;
import com.youpai.platform.chat.web.entity.req.RecallMessageReq;
import com.youpai.platform.chat.web.entity.req.SyncMsgListRequest;
import com.youpai.platform.chat.web.entity.resp.RecallMessageResp;
import com.youpai.platform.common.enums.ProcessCodeEnum;
import com.youpai.platform.common.exceptions.BaseBizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @date: 2022/7/17 17:34
 * @author: Anna
 * @version: 1.0
 * @description: 聊天消息服务
 */
@Slf4j
@Component
public class ChatMessageServiceImpl implements ChatMessageService {

    @Resource
    private ChatMessageMapper chatMessageMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public List<ChatMessageDTO> syncMsgListRequest(SyncMsgListRequest req) {
        // 获取sessionId
        Integer count = req.getCount();
        if (null == count || 0 == count) {
            count = ChatConstants.DEFAULT_SYNC_MSG_SIZE;
        }
        // 获取消息列表
        List<ChatMessagePO> chatMessagePOList = queryMsgList(req.getSessionId(), count, StringUtils.isBlank(req.getFromMsgId()) ? null : Long.valueOf(req.getFromMsgId()));
        LinkedList<ChatMessageDTO> chatMessageList = ChatMessageConverter.convertChatMessagePOList2ChatMessageDTOList(chatMessagePOList);
        // 如果消息总数大于页数说明消息没有结束，就把第一个移除掉
        if (chatMessagePOList.size() > count) {
            chatMessageList.removeFirst();
        }
        return chatMessageList;
    }

    @Override
    public RecallMessageResp recallTheMessage(RecallMessageReq req) {
        if (StringUtils.isBlank(req.getMessageId())) {
            throw new BaseBizException(ProcessCodeEnum.PARAM_ERROR);
        }
        String messageId = req.getMessageId();
        String recallMessageKey = ChatMessageRedisKeyUtil.getRecallMessageCacheKey(messageId);
        Long expire = redisTemplate.opsForValue().getOperations().getExpire(recallMessageKey);
        if (null == expire || expire < 0) {
            throw new BaseBizException(ProcessCodeEnum.MESSAGE_RECALL_OVERTIME);
        }
        RecallMessageResp resp = new RecallMessageResp();
        Integer removeResult = chatMessageMapper.removeMessage(Long.parseLong(messageId));
        if (1 != removeResult) {
            resp.setStatus(0)
                    .setHintText(ChatConstants.RECALL_MESSAGE_FAIL_HINT_TEXT);
            return resp;
        }
        resp.setStatus(1)
                .setHintText(ChatConstants.RECALL_MESSAGE_HINT_TEXT);
        return resp;
    }

    private List<ChatMessagePO> queryMsgList(String sessionId, Integer count, Long fromMsgId) {
        return chatMessageMapper.queryMsgList(sessionId, fromMsgId, count);
    }
}
